package com.wang.tool.dagre;

import com.wang.tool.dagre.node.AbsDagreNode;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.Executors;

class Demo {

    public static void main(String[] args) {
//        构建引擎
//        可动态构建，可初始化构建注册入spring

        DagreEngine<String> engine = new DagreEngine<>();
        engine.setName("测试图流程");
//        添加流程节点，配置依赖
        engine.addNode(new AbsDagreNode<String>() {
            @Override
            protected Object doPreCheck(DagreContext<String> context) throws Throwable {
//                前置验证
                return null;
            }

            @Override
            protected Object doExecute(DagreContext<String> context) throws Throwable {
                return null;
            }

            @Override
            protected Object handleError(DagreContext<String> context, Throwable e) {
                return null;
            }

            @Override
            public String getId() {
                return "步骤1";
            }

            @Override
            public Collection<String> getDependIds() {
                return null;
            }

            @Override
            public boolean isSync() {
                return true;
            }
        });

        engine.addNode(new AbsDagreNode<String>() {
            @Override
            protected Object doPreCheck(DagreContext<String> context) throws Throwable {
//                前置验证
                return null;
            }

            @Override
            protected Object doExecute(DagreContext<String> context) throws Throwable {
                System.out.println("步骤2");
                Thread.sleep(100);
                return "步骤2结果";
            }

            @Override
            protected Object handleError(DagreContext<String> context, Throwable e) {
                return null;
            }

            @Override
            public String getId() {
                return "步骤2";
            }

            @Override
            public Collection<String> getDependIds() {
                return Arrays.asList("步骤1");
            }

            @Override
            public boolean isSync() {
                return false;
            }
        });

        engine.addNode(new AbsDagreNode<String>() {
            @Override
            protected Object doPreCheck(DagreContext<String> context) throws Throwable {
//                前置验证
                return null;
            }

            @Override
            protected Object doExecute(DagreContext<String> context) throws Throwable {
                System.out.println("步骤3");
                Thread.sleep(100);
                return "步骤3结果";
            }

            @Override
            protected Object handleError(DagreContext<String> context, Throwable e) {
                return null;
            }

            @Override
            public String getId() {
                return "步骤3";
            }

            @Override
            public Collection<String> getDependIds() {
                return Arrays.asList("步骤1");
            }

            @Override
            public boolean isSync() {
                return false;
            }
        });

//        配置需要执行的节点选择器，所有节点都执行
        engine.setNodeSelector(new NodeSelector<String>() {
            @Override
            public Set<String> selector(DagreEngine<String> engine, String param, Integer timeout) {
                return engine.getAllIds();
            }
        });

//        设置并行线程池
        engine.setMainExecutor(Executors.newFixedThreadPool(3));


//        执行
        DagreResult result = engine.run("参数", 400);

        System.out.println("是否成功"+result.isSuccess());
        System.out.println("耗时"+result.getCost());
        System.out.println("获取结果"+result.getData());
        System.out.println("获取执行的节点运行状态/结果:"+result.getNodeStates());


//        图化结果
        System.out.println(engine.drawDagre(result));


    }
}